﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace YJY_DMZS.BasisFile
{
    public partial class FrmDepartment_Editor : XtraForm
    {
        public int ParentID; 

        public FrmDepartment_Editor()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 部门ID,非整数位新增
        /// </summary>
        public int ID;
        /// <summary>
        /// 部门类型ID
        /// </summary>
        private int TypeID;
        /// <summary>
        /// 部门类型编号
        /// </summary>
        private string TypeNo;

        private string captionFormat;

        private void FrmDepartment_Editor_Load(object sender, EventArgs e)
        {
            captionFormat = Text;
        }

        private void txtCode_EditValueChanged(object sender, EventArgs e)
        {
        }

        private void FrmDepartment_Editor_Shown(object sender, EventArgs e)
        {
            if (ID <= 0)
            {
                NewRecord();
            }
            else
            {
                LoadData();
            }
        }
        /// <summary>
        /// 加载数据
        /// </summary>
        private void LoadData()
        {
            Text = string.Format(captionFormat, "维护");
            var bll = new DMZS.BLL.DepartmentNew();
            var row = bll.GetRecordByID(ID);
            if (row == null)
            {
                XtraMessageBox.Show("该条数据已被删除，请刷新数据。", Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                Close();
                return;
            }

            TypeID = Convert.ToInt32(row["TypeID"]);
            TypeNo = row["TypeNo"].ToString();
            bteTypeNo.Text = row["TypeNo"].ToString();
            txtTypeName.Text = row["TypeName"].ToString();
            txtCode.Text = row["Code"].ToString();
            txtName.Text = row["Name"].ToString();
            txtFullName.Text = row["FullName"].ToString();
            mmeRemark.Text = row["Remark"].ToString();

            bteTypeNo.Properties.ReadOnly = true;
            bteTypeNo.TabStop = false;
            bteTypeNo.Properties.Buttons[0].Enabled = false;
            txtCode.Properties.ReadOnly = true;
            txtName.Focus();
        }

        private void NewRecord()
        {
            Text = string.Format(captionFormat, "新增");
            ID = 0;
            TypeID = 0;
            TypeNo = string.Empty;
            bteTypeNo.Text = string.Empty;
            txtTypeName.Text = string.Empty;
            txtCode.Text = string.Empty;
            txtName.Text = string.Empty;
            txtFullName.Text = string.Empty;
            mmeRemark.Text = string.Empty;

            txtCode.Properties.ReadOnly = false;
            bteTypeNo.TabStop = true;
            bteTypeNo.Properties.ReadOnly = false;
            bteTypeNo.Properties.Buttons[0].Enabled = true;
            bteTypeNo.Focus();
        }
        private void btnSave_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            bteTypeNo.Leave -= new EventHandler(bteTypeNo_Leave);
            txtCode.Focus();
            bteTypeNo.Leave += new EventHandler(bteTypeNo_Leave);

            if (!(ValidateDepartmentType() && ValidateInput()))
            {
                return;
            }
            if (ID <= 0)
            {
                SavaNew();
            }
            else
            {
                SaveModify();
            }
        }
        /// <summary>
        /// 保存修改
        /// </summary>
        private void SaveModify()
        {
            var bll = new DMZS.BLL.DepartmentNew();
            var model = new DMZS.Model.DepartmentNew();
            model.ID = ID;
            model.TypeID = TypeID;
            model.Code = txtCode.Text.Trim();
            model.Name = txtName.Text.Trim();
            model.NamePY = CommonControl.StringHelper.GetFirstLetterOfChinese(model.Name, true);
            model.FullName = txtFullName.Text.Trim();
            model.Remark = mmeRemark.Text;

            ProcessesResult(bll.UpdateExpand(model));
        }
        /// <summary>
        /// 新记录
        /// </summary>
        private void SavaNew()
        {
            var bll = new DMZS.BLL.DepartmentNew();
            var model = new DMZS.Model.DepartmentNew();
            model.TypeID = TypeID;
            model.Code = txtCode.Text.Trim();
            model.Name = txtName.Text.Trim();
            model.NamePY = CommonControl.StringHelper.GetFirstLetterOfChinese(model.Name, true);
            model.FullName = txtFullName.Text.Trim();
            model.Remark = mmeRemark.Text;

            ProcessesResult(bll.AddExpand(model));
        }
        /// <summary>
        /// 处理保存结果
        /// </summary>
        /// <param name="p"></param>
        private void ProcessesResult(int result)
        {
            if (result > 0)
            {
                XtraMessageBox.Show("保存成功。", Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                ID = result;
                LoadData();
            }
            else
            {
                if (result == -1000)
                {
                    XtraMessageBox.Show("编号重复。", Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    txtCode.Focus();
                    txtCode.SelectAll();
                }
                else
                {
                    if (result == -1001)
                    {
                        XtraMessageBox.Show("该部门类型不存在。", Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                        bteTypeNo.Focus();
                        bteTypeNo.SelectAll();
                    }
                    else
                    {
                        ID = result;
                        LoadData();
                    }
                }
            }
        }
        /// <summary>
        /// 验证输入
        /// </summary>
        /// <returns></returns>
        private bool ValidateInput()
        {
            if (bteTypeNo.Text.Trim().Length == 0)
            {
                XtraMessageBox.Show("请输入类型编号。", Text, MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                bteTypeNo.Focus();
                return false;
            }
            else
            {
                if (txtCode.Text.Trim().Length == 0)
                {
                    XtraMessageBox.Show("请输入部门编号。", Text, MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                    txtCode.Focus();
                    return false;
                }
                else
                {
                    if (txtName.Text.Trim().Length == 0)
                    {
                        XtraMessageBox.Show("请输入部门名称。", Text, MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                        txtName.Focus();
                        return false;
                    }
                }
            }
            return true;
        }

        private void bteTypeNo_Leave(object sender, EventArgs e)
        {
            if (TypeNo == bteTypeNo.Text.Trim())
            {
                return;
            }
            ValidateDepartmentType();
        }

        /// <summary>
        /// 验证类型
        /// </summary>
        /// <returns></returns>
        private bool ValidateDepartmentType()
        {
            var bll = new DMZS.BLL.DepartmentType();
            var row = bll.GetRecordByNo(bteTypeNo.Text.Trim());
            if (row == null)
            {
                XtraMessageBox.Show("该部门类型不存在。", Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                TypeID = 0;
                TypeNo = string.Empty;
                txtTypeName.Text = string.Empty;
                bteTypeNo.Focus();
                bteTypeNo.SelectAll();
                return false;
            }
            else
            {
                TypeID = Convert.ToInt32(row["ID"]);
                TypeNo = row["No"].ToString();
                txtTypeName.Text = row["Name"].ToString();
                return true;
            }
        }
        private void btnDelete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (ID <= 0)
            {
                NewRecord();
                return;
            }
            else
            {
                if (DialogResult.Yes == XtraMessageBox.Show("确认删除?", Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1))
                {
                    var bll = new DMZS.BLL.DepartmentNew();
                    bll.Delete(ID);
                    NewRecord();
                }
            }
        }
        private void btnRefresh_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (ID <= 0)
            {
                NewRecord();
            }
            else
            {
                LoadData();
            }
        }
        private void btnFirst_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var bll = new DMZS.BLL.DepartmentNew();
            var row = bll.GetFirst();
            if (row != null)
            {
                ID = Convert.ToInt32(row["ID"]);
                LoadData();
            }
            else
            {
                NewRecord();
            }
        }
        private void btnPrevious_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (ID <= 0)
            {
                return;
            }
            var bll = new DMZS.BLL.DepartmentNew();
            var row = bll.GetPrevious(ID);
            if (row != null)
            {
                ID = Convert.ToInt32(row["ID"]);
                LoadData();
            }
            else
            {
                LoadData();
            }
        }
        private void btnNext_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (ID <= 0)
            {
                return;
            }
            var bll = new DMZS.BLL.DepartmentNew();
            var row = bll.GetNext(ID);
            if (row != null)
            {
                ID = Convert.ToInt32(row["ID"]);
                LoadData();
            }
            else
            {
                LoadData();
            }
        }
        private void btnLast_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var bll = new DMZS.BLL.DepartmentNew();
            var row = bll.GetLast();
            if (row != null)
            {
                ID = Convert.ToInt32(row["ID"]);
                LoadData();
            }
            else
            {
                NewRecord();
            }
        }
        private void btnNew_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            NewRecord();
        }
    }
}
